package com.jp.knowledge.adapter.daily_knowledge;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.command.Command;
import com.jp.knowledge.command.CommandInvoker;
import com.jp.knowledge.command.CommandReceiver;
import com.jp.knowledge.model.DailyKnowledgePackageModel;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;

import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/7/25.
 */

public class DailyKnowledgePackageListAdapter extends RecycleVAdapter<DailyKnowledgePackageModel.ListBean> implements RecycleVAdapter.OnItemClickListener {

	private int direction;

	public int getDirection() {
		return direction;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}

	public DailyKnowledgePackageListAdapter(Context context, @NonNull List<DailyKnowledgePackageModel.ListBean> list, int direction) {
		super(context, list);
		this.direction = direction;
		addItemClickListener(this);
	}

	@Override
	public int getLayoutId(int viewType) {
		return R.layout.item_daily_knowledge_list;
	}

	@Override
	public void onBind(RecycleVHolder holder, int position) {
		DailyKnowledgePackageModel.ListBean data = getData(position);
		holder.setTextView(R.id.knowledge_title, data.getTitle());
		List<DailyKnowledgePackageModel.ListBean.ImglistBean> imglistBean = data.getImglist();
		String imgPath = null;
		if(imglistBean != null && imglistBean.size() > 0 && imglistBean.get(0) != null)
			imgPath = imglistBean.get(0).getSrc();
		if(direction == 1 && imgPath != null){
			holder.get(R.id.knowledge_left_img).setVisibility(View.VISIBLE);
			holder.get(R.id.knowledge_right_img).setVisibility(View.GONE);
			holder.setImageUrl(R.id.knowledge_left_img, imgPath);
		} else if(direction == 2 && imgPath != null){
			holder.get(R.id.knowledge_left_img).setVisibility(View.GONE);
			holder.get(R.id.knowledge_right_img).setVisibility(View.VISIBLE);
			holder.setImageUrl(R.id.knowledge_right_img, imgPath);
		} else{
			holder.get(R.id.knowledge_left_img).setVisibility(View.GONE);
			holder.get(R.id.knowledge_right_img).setVisibility(View.GONE);
		}
	}

	@Override
	public void itemSelect(int position) {
		DailyKnowledgePackageModel.ListBean data = getData(position);
		CommandReceiver commandReceiver = new CommandReceiver.Builder(mContext)
				.withRecordId(data.getRecordId())
				.withParameterId(data.getParameterId() + "")
				.build();
		Command commandObject = Command.create(data.getCommand(), commandReceiver);
		CommandInvoker.executeCommand(commandObject);
	}
}
